package com.shanks.home.module.android;

import java.text.SimpleDateFormat;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shanks.home.R;
import com.shanks.home.util.time.AdvancedCountdownTimer;
import com.shanks.home.util.time.LocalTime;

public class TimeActivity extends Activity {
	
	private Button startBtn, pauseBtn, resumeBtn, localTimeBtn;
	private TextView showTV;
	private AdvancedCountdownTimer countdownTimer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.android_time_activity);
		
		initView();
	}
	
	private void initView() {
		startBtn = (Button)this.findViewById(R.id.android_time_start_btn);
		pauseBtn = (Button)this.findViewById(R.id.android_time_pause_btn);
		resumeBtn = (Button)this.findViewById(R.id.android_time_resume_btn);
		localTimeBtn = (Button)this.findViewById(R.id.android_time_loacl_time_btn);
		showTV = (TextView)this.findViewById(R.id.android_time_show_tv);
		
		startBtn.setOnClickListener(onClickListener);
		pauseBtn.setOnClickListener(onClickListener);
		resumeBtn.setOnClickListener(onClickListener);
		localTimeBtn.setOnClickListener(onClickListener);
	}
	
	private OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if (v == startBtn) {
				start();
			} else if (v == pauseBtn) {
				pause();
			} else if (v == resumeBtn) {
				resume();
			} else if (v == localTimeBtn) {
				localTime();
			}
		}
	};
	
	private void start() {
		countdownTimer = new AdvancedCountdownTimer(25 * 1000, 1000) {
			
			@Override
			public void onTick(long millisUntilFinished, int percent) {
				SimpleDateFormat sdf = new SimpleDateFormat("mm:ss", Locale.getDefault());
				String str = sdf.format(millisUntilFinished);
				
				showTV.setText(str);
			}
			
			@Override
			public void onFinish() {
				showTV.setText("finish!");
			}
		};
		countdownTimer.start();
	}
	
	private void pause() {
		countdownTimer.pause();
	}
	
	private void resume() {
		countdownTimer.resume();
	}
	
	private void localTime() {
		LocalTime.startGetTime();
	}
}